var http = require('http'), https = require('https');
var iconv = require('iconv-lite');
function getHttpContent(url, callback) {
	http.get(url, function(res) {
		var statusCode = res.statusCode;
		if (statusCode == 200) {
			res.on('data', callback.data);
			res.on('end', callback.end);
		} else if (statusCode == 302) {
			var headers = res.headers;
			var location = headers['location'];
			getHttpContent(location, callback);
		}
	})
}

exports.get = function(url, callback) {
	getHttpContent(url, callback)
}

exports.getContent = function(url, charset, contentCallback) {
	var dataBuf = new Buffer('');
	var getCallback = {
		'data' : function(data) {
			dataBuf = Buffer.concat([ dataBuf, data ]);
		},
		'end' : function() {
			if (typeof charset == 'undefined' || charset == null) {
				charset = 'utf-8';
			}
			var response = iconv.decode(dataBuf, charset);
			contentCallback(response);
		}
	};
	getHttpContent(url, getCallback);

}